package com.socialuni.jimei.config;

import com.socialuni.sdk.model.DO.user.AccountDO;
import com.socialuni.sdk.utils.CommonTokenUtil;
import com.socialuni.sdk.config.SocialFeignHeaderName;
import com.socialuni.sdk.utils.model.SocialUserAccountUtil;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.stereotype.Component;

@Slf4j
@Component
@ConditionalOnClass(RequestInterceptor.class)
public class FeignInterceptor implements RequestInterceptor {
    @Value("${social.secret-key}")
    private String socialSecretKey;

    @Override
    public void apply(RequestTemplate requestTemplate) {
        //对服务调用进行签名，当前签名方法比较简单，以后可以支持更加复杂的签名计算（比如读取参数内容，组合后再进行签名计算）
//        requestTemplate.header("appId", authProperties.getAppId());
        requestTemplate.header(SocialFeignHeaderName.socialSecretKey, this.socialSecretKey);
        if (CommonTokenUtil.hasToken()) {
            //走了这里一定是绑定了社交联盟账号，获取社交联盟账户
            AccountDO socialUserAccountDO = SocialUserAccountUtil.getMineSocialAccount();
            if (socialUserAccountDO != null) {
                String uniToken = socialUserAccountDO.getSessionKey();
                requestTemplate.header(SocialFeignHeaderName.socialToken, uniToken);
            }
        }
//        requestTemplate.header("sign", md5);
        //一些接口的调用需要实现幂等，比如消息发送，如果使用requestId就可以方便服务方实现幂等
//        requestTemplate.header("requestId", UUID.randomUUID().toString().replaceAll("-", ""));
    }
}